package com.zzyl.nursing.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.mapper.FamilyMemberMapper;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.service.WechatService;
import com.zzyl.nursing.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;

@Service
public class FamilyMemberServiceImpl extends ServiceImpl<FamilyMemberMapper, FamilyMember> implements IFamilyMemberService
{

    @Autowired
    private WechatService wechatService;
    @Autowired
    private TokenService tokenService;

    static List<String> DEFAULT_NICKNAME_PREFIX = ListUtil.of("生活更美好",
            "大桔大利",
            "日富一日",
            "好柿开花",
            "柿柿如意",
            "一椰暴富",
            "大柚所为",
            "杨梅吐气",
            "天生荔枝"
    );
    /**
     * 微信登录
     *
     * @param dto
     * @return
     */
    @Override
    public LoginVo login(UserLoginRequestDto dto) {
        //1.根据code调用微信api获取apenId
        String openid = wechatService.getOpenid(dto.getCode());

        //2.根据openId查询数据库获取用户信息family_member表
        FamilyMember familyMember = this.getOne(Wrappers.<FamilyMember>lambdaQuery()
                .eq(FamilyMember::getOpenId, openid));

        //3.判断用户信息是否存在，如果不存在，则新增family_member表
        if (ObjectUtils.isEmpty(familyMember)){
            familyMember=FamilyMember.builder().openId(openid).build();

        }

        //4.调用微信api获取用户绑定手机号
        String phone = wechatService.getPhone(dto.getPhoneCode());

        //5.新增或者修改family_member表数据
        saveOrUpdateFamilyMember(familyMember,phone);
//        update(Wrappers.<FamilyMember>lambdaUpdate().eq(FamilyMember::getId,phone));


        HashMap<String, Object> claims = new HashMap<>();
        claims.put("userId",familyMember.getId());
        claims.put("nickName",dto.getNickName());
        //6.将用户id存入token中，并返回数据给前端
        String token = tokenService.createToken(claims);
        LoginVo loginVo = new LoginVo();
        loginVo.setNickName(familyMember.getName());
        loginVo.setToken(token);


        return loginVo;
    }

    /**
     * 新增或修改数据
     * @param familyMember
     * @param phone
     */
    private void saveOrUpdateFamilyMember(FamilyMember familyMember, String phone) {

        //如果手机号不相等，就修改
//        if (familyMember.getPhone().equals(phone)){
        if (ObjectUtil.notEqual(phone, familyMember.getPhone())) {
                familyMember.setPhone(phone);
        }

        if (ObjectUtils.isEmpty(familyMember.getId())){
            //新增
            //生成用户名
            familyMember.setName(DEFAULT_NICKNAME_PREFIX.get((int) (Math.random()*DEFAULT_NICKNAME_PREFIX.size()))+ StringUtils.substring(familyMember.getPhone(), 7));
            save(familyMember);
        }

        //修改
        updateById(familyMember);
    }
}